package com.mentalhealthplatform.teacherV2.service;

import com.mentalhealthplatform.appModels.Message;
import com.mentalhealthplatform.teacherV2.repository.MessageRepositoryV2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Service
public class MessageServiceV2 {

    @Autowired
    private MessageRepositoryV2 messageRepository;

    // 获取所有消息
    public List<Message> findAll() {
        return messageRepository.findAll();
    }

    // 根据ID获取消息
    public Optional<Message> findById(Long id) {
        return messageRepository.findById(id);
    }

    // 保存消息
    public Message save(Message message) {
        return messageRepository.save(message);
    }

    // 删除消息
    public void deleteMessage(Long id) {
        messageRepository.deleteById(id);
    }

    // 标记消息为已读
    public void markAsRead(Long messageId) {
        Optional<Message> messageOptional = messageRepository.findById(messageId);
        if (messageOptional.isPresent()) {
            Message message = messageOptional.get();
            message.setRead(true);
            messageRepository.save(message);
        }
    }

    // 点赞消息
    public void likeMessage(Long messageId, UUID teacherId) {
        Optional<Message> messageOptional = messageRepository.findById(messageId);
        if (messageOptional.isPresent()) {
            Message message = messageOptional.get();
            message.setLikes(message.getLikes() + 1);
            message.setLikedByCurrentTeacher(true);
            messageRepository.save(message);
        }
    }
}